home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.01 Jan 96 / 12.01 Tips & Tidbits / FSpTrashFile / FSpTrashFile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-11  |  2.3 KB  |  112 lines  |  [TEXT/MMCC]

  1. /******************************************************************************
  2.      FSpTrashFile.c
  3.     
  4.     Move a file to the trash if it is not possible to delete it.
  5.     Can only move file to the trash if it is on the same volume
  6.     as the file to be deleted.
  7.  
  8.     history:
  9.  
  10.         modified:    xx/xx/xx        who are you? what did you do?
  11.         created:    11/23/94        greg poole
  12.  
  13.     Greg Poole
  14.     Vital Images, Inc.
  15.     505 N. 4th Street
  16.     Fairfield, IA 52556
  17.     (515) 472-7726
  18.     email: greg@vitalimages.com
  19.  
  20.  ******************************************************************************/
  21.  
  22. #include <Folders.h>
  23. #include "FSpTrashFile.h"
  24.  
  25.  
  26. OSErr FSpTrashFile( FSSpecPtr theFile )
  27. {
  28.     OSErr        theErr = noErr;
  29.     short        vRefNum;
  30.     long        dirID;
  31.     FSSpec        spec;
  32.     FSSpecPtr    theTrash = &spec;
  33.     
  34.     // don't go any further if there isn't a file to deal with or if the
  35.     // file was deleted successfully
  36.     //
  37.     theErr = FSpDelete( theFile );
  38.     if ( theErr == fnfErr || theErr == noErr )
  39.         return noErr;
  40.         
  41.     // if we got this far, we had a problem deleting the file,
  42.     // don't worry, relax and just trash it
  43.     //
  44.     theErr = FindFolder( kOnSystemDisk, kTrashFolderType, kDontCreateFolder, &vRefNum, &dirID );
  45.         
  46.     if ( theErr == noErr )
  47.         theErr = FSMakeFSSpec( vRefNum, dirID, "\p", theTrash );
  48.  
  49.     if ( theErr == noErr )
  50.         if ( theFile->vRefNum != theTrash->vRefNum )
  51.             theErr = diffVolErr;
  52.     
  53.     if ( theErr == noErr )
  54.         theErr = CatMove( theFile->vRefNum, theFile->parID, theFile->name, 
  55.                     theTrash->parID, theTrash->name );
  56.  
  57.     return theErr;
  58.     
  59. } // end FSpTrashFile
  60.  
  61.  
  62. // define TEST_TRASH_FILE for a standalone test
  63. //
  64. #define TEST_TRASH_FILE
  65.  
  66. #if defined( TEST_TRASH_FILE )
  67.  
  68.     // local function prototypes
  69.     //
  70.     static void     InitTheMac( void );
  71.     static Boolean     GetAFile( FSSpecPtr file );
  72.     
  73.     static void InitTheMac( void )
  74.     {
  75.         InitGraf( &qd.thePort );
  76.         InitFonts();
  77.         InitWindows();
  78.         InitMenus();
  79.         TEInit();
  80.         InitDialogs( 0L );
  81.         InitCursor();
  82.         MaxApplZone();
  83.     
  84.     } // end InitTheMac
  85.     
  86.     static Boolean GetAFile( FSSpecPtr file )
  87.     {
  88.         SFTypeList            types;
  89.         StandardFileReply    reply;
  90.         
  91.         StandardGetFile( NULL, -1, types, &reply );
  92.         if ( reply.sfGood )
  93.             BlockMove( &reply.sfFile, file, sizeof( FSSpec ) );
  94.     
  95.         return reply.sfGood;
  96.     
  97.     } // end GetAFile
  98.  
  99.     void main( void )
  100.     {
  101.         FSSpec         srcFileSpec;
  102.         OSErr         theErr = noErr;
  103.         
  104.         InitTheMac();
  105.         if ( GetAFile( &srcFileSpec ) )
  106.             theErr = FSpTrashFile( &srcFileSpec );
  107.  
  108.     } // end main
  109.  
  110. #endif // TEST_TRASH_FILE
  111.  
  112.